import { create } from 'zustand'

interface User {
  name: string
  role: string
}

interface ChartData {
  sales: number[]
  categories: string[]
}

interface AppState {
  user: User
  theme: 'light' | 'dark'
  loading: boolean
  chartData: ChartData
  setTheme: (theme: 'light' | 'dark') => void
  setLoading: (loading: boolean) => void
  setChartData: (data: ChartData) => void
  updateUser: (userData: Partial<User>) => void
  reset: () => void
}

// 主应用状态管理
export const useAppStore = create<AppState>((set, get) => ({
  // 用户信息
  user: {
    name: '管理员',
    role: 'admin'
  },
  
  // 主题设置
  theme: 'light',
  setTheme: (theme) => set({ theme }),
  
  // 加载状态
  loading: false,
  setLoading: (loading) => set({ loading }),
  
  // 图表数据
  chartData: {
    sales: [120, 200, 150, 80, 70, 110, 130],
    categories: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
  },
  setChartData: (data) => set({ chartData: data }),
  
  // 更新用户信息
  updateUser: (userData) => set((state) => ({
    user: { ...state.user, ...userData }
  })),
  
  // 重置状态
  reset: () => set({
    user: { name: '管理员', role: 'admin' },
    theme: 'light',
    loading: false,
    chartData: {
      sales: [120, 200, 150, 80, 70, 110, 130],
      categories: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
    }
  })
}))